<?php
namespace jy_newping;
defined('BY_JYA') or exit('error');
/*
	任务解锁
*/
class renwu extends \plugin{
	/*
		downs
	*/
	public function canyu($params,$pinggoods=array()){
		$fromuser=pdo_getcolumn('jy_newping_member',array('mid'=>$params['mid'],'uniacid'=>$_SESSION['uniacid']),'fromuser');
		$isnew=pdo_count('jy_newping_log',array('status'=>1,'type'=>0,'mid' =>$params['mid']),'id');
		//第一次参与，有释放虚拟币机会！
		if($isnew==1){
			$this->up_daycanyus($fromuser);
		}
		$basic=$this->plugin_setting('basic');
		if($basic['task_open']!=1){
			return false;
		}
		//升级
		$this->model('member')->update_agent_level($fromuser);
		//自购升级
		$this->model('member')->update_agent_level($params['mid'],true,$pinggoods);
		//福利解锁
		$this->task_fuup($fromuser);
		return false;
	}
	public function canfen($params,$pinggoods=array()){
		$basic=$this->plugin_setting('basic');
		if($basic['task_open']!=1){
			return false;
		}
		$this->pinggoods=$pinggoods;
		$this->commission_order=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$params['mid'],
			'orderid'=>$params['id'],
			'order_price'=>$pinggoods['price'],
			'goodscommissions'=>json_decode($pinggoods['commissions'],true),
			'commission_money'=>0,
			'team_money'=>0,
			'red_money'=>0,
			'fen_futype'=>$basic['fen_futype']?:2,
		);
		$member=pdo_get('jy_newping_member',array('mid'=>$params['mid'],'uniacid'=>$_SESSION['uniacid']));
		$fromuser=$member['fromuser'];
		if($member['level_id']){
			$lastlevel=$this->getcolumn('level',array('id'=>$member['level_id']),'level');
		}else{
			$lastlevel=0;
		}
		$parameter=array(
			'params'=>array('uniacid'=>$_SESSION['uniacid']),
			'orderby'=>'level asc',
			'limit'=>'1',
		);		
		$levels=$this->get_list('level',$parameter);
		$minlevel=$levels[0]['level'];
		$parameter['orderby']='level desc';
		$levels=$this->get_list('level',$parameter);
		$this->commission_order['maxlevel']=$maxlevel=$levels[0]['level'];
		$levelnum=9;		
		//极差分红单线等级ABCDE，第一级没有分红，低于自己的没有分红，平级没有分红
		for($i=0;$i<$levelnum;$i++){
				if(!$fromuser){
					break;
				}
				$member=$this->get('member',array('mid'=>$fromuser,'uniacid'=>$_SESSION['uniacid']));
				$level=$this->get('level',array('id'=>$member['level_id']));
				if($i<=1){
					//直推，间推
					$this->create_commission($member,$level,$i);
				}
				$level['level']=$level['level']?:0;
				$fromuser=$member['fromuser'];
				//当等级匹配时，分红
				$this->lastlevel=$level['level'];
				//团队奖励
				$this->create_teamcommission($member,$level);
		}
		//合伙人
		$levels=$this->pdo_getall('level',array('uniacid'=>$_SESSION['uniacid']));
		foreach($levels as $level){
			$this->create_redcommission($level);
		}
		return false;
	}
	/*
		分销佣金;
	*/
	function create_commission($agent,$level=array(),$i=0){
			$commission_level=$agent['level_id'];
			if(!$agent['level_id']){
				return false;
			}else{
				$level_id=$level['id'];
				$commissions=json_decode($level['commissions'],true);
				$commission_per=$commissions['commission_'.$i];
			}
			
			$money=0;
			$order_price=$this->commission_order['order_price'];		
			//先检查是否存在商品独立分红规则，如果存在则对订单相应商品部分金额单独计算分红
			$goods_commission=$this->commission_order['goodscommissions'];
			foreach($goods_commission as $gv){
				if($agent['level_id']==$gv['level_id']){
					$goods_commission_per=$gv['commissions']['commission_'.$i];
					$isgoodsper=1;
					if(substr($goods_commission_per,-1)=='%'){
							$money=intval($order_price*substr($goods_commission_per,0,-1))/100;
					}else{
							$money=$goods_commission_per;
					}
					break;
				}
			}	
			if(!$isgoodsper){
				if(substr($commission_per,-1)=='%'){
					$money=intval($order_price*substr($commission_per,0,-1))/100;
				}else{
					$money=$commission_per;
				}
			}
			if($money>0){
				$new_commission=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$agent['mid'],
					'level'=>$level_id,
					'orderid'=>$this->commission_order['orderid'],
					'money'=>$money,
					'createtime'=>TIMESTAMP,
					'status'=>0,
					'type'=>$i,
					'fen_futype'=>$this->commission_order['fen_futype'],
				);
				$this->insert('commission',$new_commission);
				if($i==0){
					//拿自己上级的佣金
					$this->create_shangjifee($money);
				}
			}
			return true;
	}
	/*
		级差分佣type=1;
	*/
	function create_teamcommission($agent,$level=array()){
			$commission_level=$agent['level_id'];
			if(!$agent['level_id']){
				//获取默认等级分红比例
				return false;
			}else{
				$level['commissions']=json_decode($level['commissions'],true);
				$level_id=$level['id'];
				$commission_per=$level['commissions']['teamfee'];
				$red_per=$level['commissions']['redfee'];
			}
			$money=0;
			$red_money=0;
			$order_price=$this->commission_order['commission_price'];
			$remark=array(
				'level_id'=>$level_id,
				'order_price'=>$order_price,
				'goods_commission'=>array(),
			);			
			//先检查是否存在商品独立分红规则，如果存在则对订单相应商品部分金额单独计算分红
			$teamfee_level='teamfee'.$level_id;
			$redfee_level='redfee'.$level_id;
			if($this->pinggoods['ignore']  || !$this->pinggoods['id']){
					$goods_commission=array();
					if($this->pinggoods['ignore']){
						$order_price=$order_price-$total_price;
					}
					$this->needorder=true;
			}else{
					$goods_commission=$this->pinggoods['commissions']?json_decode($this->pinggoods['commissions'],true):array();
					foreach($goods_commission as $gv){
						if($agent['level_id']==$gv['level_id']){
							$goods_commission_per=$gv['commissions']['teamfee'];
							break;
						}
					}
			}
			$order_price=$order_price-$this->pinggoods['price'];
			//团队奖励			
			if(substr($goods_commission_per,-1)=='%'){
					$goods_commission_money=intval($this->pinggoods['price']*substr($goods_commission_per,0,-1))/100;
					$goods_commission_money -=$this->commission_order['team_money'];
					$money+=$goods_commission_money;
			}else{
					$goods_commission_money=$goods_commission_per;
					$goods_commission_money -=$this->commission_order['team_money'];
					$money+=$goods_commission_money;
			}
			$remark['goods_commission'][]=array('goods_id'=>$this->pinggoods['id'],'total_price'=>$this->pinggoods['price'],'total'=>1,'commission_per'=>$goods_commission_per,'commission_money'=>$goods_commission_money);
			if($order_price>0 && $this->needorder){
				if(substr($commission_per,-1)=='%'){
					$order_commission_money=intval($order_price*substr($commission_per,0,-1))/100;
					$order_commission_money -=$this->commission_order['team_money'];
					$money+=$order_commission_money;
				}else{
					$order_commission_money=$commission_per;
					$order_commission_money -=$this->commission_order['team_money'];
					$money+=$order_commission_money;
				}
				$remark['order_commission']=array('order_price'=>$order_price,'commission_per'=>$commission_per,'commission_money'=>$order_commission_money);
			}
			$remark['commission_money']=$money;
			//团队奖励
			if($money>0 && $level['level']>$this->lastlevel){
				$this->commission_order['team_money']+=$money;
				$new_commission=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$agent['mid'],
					'level'=>$level['level'],
					'orderid'=>$this->commission_order['orderid'],
					'money'=>$money,
					'createtime'=>TIMESTAMP,
					'status'=>0,
					'type'=>3,
				);
				$this->insert('commission',$new_commission);
			}
			$this->model('member')->update_agent_level($agent['id']);
	}
	/*
		合伙人;
	*/
	function create_redcommission($level=array()){
			if(!$level['id']){
				//获取默认等级分红比例
				return false;
			}else{
				$level['commissions']=json_decode($level['commissions'],true);
				$level_id=$level['id'];
				$red_per=$level['commissions']['redfee'];
			}
			$money=0;
			$red_money=0;
			$order_price=$this->commission_order['commission_price'];
			$remark=array(
				'level_id'=>$level_id,
				'order_price'=>$order_price,
				'goods_commission'=>array(),
			);			
			//先检查是否存在商品独立分红规则，如果存在则对订单相应商品部分金额单独计算分红
			if($this->pinggoods['ignore']  || !$this->pinggoods['id']){
					$goods_commission=array();
					if($this->pinggoods['ignore']){
						$order_price=$order_price-$total_price;
					}
					$this->needorder=true;
			}else{
					$goods_commission=$this->pinggoods['commissions']?json_decode($this->pinggoods['commissions'],true):array();
					foreach($goods_commission as $gv){
						if($level_id==$gv['level_id']){
							$goods_redfee_per=$gv['commissions']['redfee'];
							break;
						}
					}
			}
			$order_price=$order_price-$this->pinggoods['price'];			
			if(substr($goods_redfee_per,-1)=='%'){
					$goods_redfee_money=intval($this->pinggoods['price']*substr($goods_redfee_per,0,-1))/100;
					$red_money +=$goods_redfee_money;
			}else{
					$goods_redfee_money=$goods_redfee_per;
					$red_money +=$goods_redfee_money;
			}
			if($order_price>0 && $this->needorder){
				if(substr($red_per,-1)=='%'){
					$order_red_money=intval($order_price*substr($red_per,0,-1))/100;
					$red_money +=$order_red_money;
				}else{
					$order_red_money=$red_per;
					$red_money +=$order_red_money;
				}
			}
			$remark['commission_money']=$money;
			if($red_money>0){
				$members=$this->getall('member',array('level_id'=>$level_id,'uniacid'=>$_SESSION['uniacid']));
				$num=count($members);
				if($num){
					$fee=$red_money/$num;
				}
				$remark['order_commission']=array('num'=>$num,'order_price'=>$order_price,'commission_per'=>$red_per,'commission_money'=>$red_money);
				foreach($members as $mv){
					$new_commission=array(
						'uniacid'=>$_SESSION['uniacid'],
						'mid'=>$mv['mid'],
						'level'=>$level['level'],
						'orderid'=>$this->commission_order['orderid'],
						'money'=>$fee,
						'createtime'=>TIMESTAMP,
						'status'=>0,
						'type'=>4,
					);
					$this->insert('commission',$new_commission);
				}
			}
	}
	/*
		上级佣金;
	*/
	function create_shangjifee($shangjifee){
			$agent=$this->get('member',array('mid'=>$this->commission_order['mid'],'uniacid'=>$_SESSION['uniacid']));
			$commission_level=$agent['level_id'];
			if(!$agent['level_id']){
				return false;
			}else{
				$level=$this->get('level',array('id'=>$agent['level_id']));
				$level_id=$level['id'];
				$commissions=json_decode($agent['commissions'],true);
				$commission_per=$commissions['shangji'];
			}
			$money=0;
			$order_price=$shangjifee;		
			//先检查是否存在商品独立分红规则，如果存在则对订单相应商品部分金额单独计算分红
			$goods_commission=$this->commission_order['goodscommissions'];
			foreach($goods_commission as $gv){
				if($agent['level_id']==$gv['level_id']){
					$goods_commission_per=$gv['shangji'];
					$isgoodsper=1;
					if(substr($goods_commission_per,-1)=='%'){
							$money=intval($order_price*substr($goods_commission_per,0,-1))/100;
					}else{
							$money=intval($goods_commission_per);
					}
					break;
				}
			}			
			if(!$isgoodsper){
				if(substr($commission_per,-1)=='%'){
					$money=intval($order_price*substr($commission_per,0,-1))/100;
				}else{
					$money=intval($commission_per);
				}
			}
			if($money>0){
				$new_commission=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$agent['mid'],
					'level'=>$level_id,
					'orderid'=>$this->commission_order['orderid'],
					'money'=>$money,
					'createtime'=>TIMESTAMP,
					'status'=>0,
					'type'=>2,
					'fen_futype'=>$this->commission_order['fen_futype'],
				);
				$this->insert('commission',$new_commission);
			}
			return true;
	}
	//团长返现
	function create_teamfan_fee($mid,$orderid=0,$pin_goods=array()){
			if($pin_goods['teamfan_fee']>0){
				$new_commission=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$mid,
					'orderid'=>$orderid,
					'money'=>$pin_goods['teamfan_fee'],
					'createtime'=>TIMESTAMP,
					'status'=>0,
					'type'=>5,
				);
				$this->insert('commission',$new_commission);
			}
			return true;
	}
	/*
		更新进入新参与;
	*/
	function up_daycanyus($mid){
		$this->model('member')->up_tixianfee($mid,true);
		$dayid=pdo_getcolumn('jy_newping_member',array('mid'=>$mid),'dayid');
		$newdayid=date('Y-m-d',TIMESTAMP);
		if($dayid==$newdayid){
			pdo_update('jy_newping_member',array('daycanyus +='=>1),array('mid'=>$mid));
		}else{
			pdo_update('jy_newping_member',array('daycanyus'=>1,'dayid'=>$newdayid),array('mid'=>$mid));
		}
		return true;
	}
	/*
		福利解锁;
	*/
	function task_fuup($mid){
		if(!$this->setting['task_funum'] || !$this->setting['task_funum'] || !$this->setting['task_funfee']){
			return false;
		}
		$isadd=pdo_getcolumn('jy_newping_feelog',array('mid'=>$mid,'type'=>2),'id');
		if($isadd){
			return false;
		}
		//推荐N人参与
		$downmids=pdo_getall('jy_newping_member',array('fromuser'=>$mid,'uniacid'=>$_SESSION['uniacid']),'mid','mid');
		if($downmids){
			$downmids=array_keys($downmids);
			$canyus=pdo_getnorepeat('jy_newping_log',array('status'=>1,'type'=>0,'mid' => $downmids),'mid');
			$downnum=count($canyus);
		}
		$type=$this->setting['task_futype']?:1;
		if($downnum>=$this->setting['task_funum']){
			model('core/member')->member_credit($mid,'credit'.$type,$this->setting['task_funfee'],true,'jy_newping','福利解锁(任务奖励类型)');
			$params=array(
				'gift_type'=>$type,
				'fee'=>$this->setting['task_funfee'],
				'msg'=>'福利解锁(任务奖励类型)',
				'status'=>1,
				'mid'=>$mid,
			);
			$this->model('model')->set_feelog($params,2);
		}
		return true;
	}
}